package com.baor.security;

import com.baor.dao.UserDao;
import com.baor.domain.UserVo;
import com.baor.domain.LoginUser;
import com.baor.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleService roleService;

    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserVo vo = userDao.selectUserByUserName(username);
        if (Objects.isNull(vo))
        {
            log.info("登录用户：{} 不存在.", username);
            throw new RuntimeException("user.not.exists");
        }
        return new LoginUser(vo,null);
    }

}
